'''
Created on Dec 27, 2011

@author: anorberg
'''

from pytsv import tsvUtils
import matplotlib.pyplot
import sys
import csv
from scipy import stats
import numpy

IN_FMT = """
Positional arguments:
    1. Pathsep-delimited glob path to input files for first data set
    2. Schema file defining schema for first data set
    3. Column to read for first data set
    4. Pathsep-delimited glob path to input files for second data set
    (5). Schema file defining schema for second data set
    (6). Column to read for second data set
    
    If there are 5 parameters, the 6th parameter is inferred to equal the third.
    If there are 4, then the 5th parameter is inferred to equal the second.
"""

if __name__ == '__main__':
    if len(sys.argv) < 5 or len(sys.argv) > 7:
        print >> sys.stderr, "Wrong number of arguments."
        print IN_FMT
        sys.exit(1)
    
    splatOne = sys.argv[1]
    schemaOne = tsvUtils.ReadSchemaFile(open(sys.argv[2], "r"))
    columnOne = sys.argv[3]
    
    splatTwo = sys.argv[4]
    
    if len(sys.argv) > 5:
        schemaTwo = tsvUtils.ReadSchemaFile(open(sys.argv[5], "r"))
    else:
        schemaTwo = schemaOne
    
    if len(sys.argv) > 6:
        columnTwo = sys.argv[6]
    else:
        columnTwo = columnOne
    
    class tabsNoQuotes(csv.excel_tab):
        doublequote = False
        skipinitialspace = True                                    
        escapechar = '\\'
        quoting=csv.QUOTE_NONE
    
    datasetOne = [float(row[columnOne]) for row in tsvUtils.WeldFiles(splatOne, schemaOne[0], schemaOne[1], tabsNoQuotes, all_headerness=False)]
    datasetTwo = [float(row[columnTwo]) for row in tsvUtils.WeldFiles(splatTwo, schemaTwo[0], schemaTwo[1], tabsNoQuotes, all_headerness=False)]
    
    ksValue, ksP = stats.ks_2samp(numpy.array(datasetOne), numpy.array(datasetTwo))
    
    print "Kolmogorov-Smirnof statistic:", str(ksValue) + ", indicating a P-value of", str(ksP)
    if ksP < 0.05:
        print "These sets were therefore probably not drawn from the same distribution."
    else:
        print "The null hypothesis has not been rejected."
        
    matplotlib.pyplot.hist([datasetOne, datasetTwo], bins=50, normed=True, log=False, label=[sys.argv[1], sys.argv[4]])
    matplotlib.pyplot.show()